export default {
	data() {
		return {
			//设置默认的分享参数
			share: {
				title: '', //标题
				path: '/pages/tabbar/index/index', //路径
				imageUrl: '', //封面
				query: "" //传递的参数，可不填，只能是这种格式id=123
			}
		}
	},
	onLoad() {
		var pages = getCurrentPages();
		var currPage = pages[pages.length - 1];
		this.share.query = this.objectToQuery(currPage.options)

        this.share.path = '/' + currPage.route + '?' + this.share.query
		console.log('页面接收参数', this.share.path)
	},
	methods: {
		objectToQuery() {
			let obj = arguments[0];
			let prefix = arguments[1];
			if (typeof obj !== "object") return "";
			const attrs = Object.keys(obj);
			return attrs.reduce((query, attr, index) => {
				// 判断是否是第一层第一个循环
				if (index === 0 && !prefix) query;
				if (typeof obj[attr] === "object") {
					const subPrefix = prefix ? `${prefix}[${attr}]` : attr;
					query += this.objectToQuery(obj[attr], subPrefix);
				} else {
					if (prefix) {
						query += `${prefix}[${attr}]=${obj[attr]}`;
					} else {
						query += `${attr}=${obj[attr]}`;
					}
				}
				// 判断是否是第一层最后一个循环
				if (index !== attrs.length - 1) query += "&";
				return query;
			}, "");
		}
	},
	onShareTimeline() {
		return {
			title: this.share.title,
			query: this.share.query,
			imageUrl: this.share.imageUrl,
			success(res) {
				uni.showToast({
					title: '分享成功'
				})
			},
			fail(res) {
				uni.showToast({
					title: '分享失败',
					icon: 'none'
				})
			}
		}
	},
	onShareAppMessage(res) {
		return {
			title: this.share.title,
			path: this.share.path,
			imageUrl: this.share.imageUrl,
			success(res) {
				uni.showToast({
					title: '分享成功'
				})
			},
			fail(res) {
				uni.showToast({
					title: '分享失败',
					icon: 'none'
				})
			}
		}
	}
}